﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dev.Control;
using sfcorp.biz;
using TT.Common;
using SfcorpDLL.ety;
using SQLHelper;

namespace Sfcorp.form
{
    public partial class frmCustomerClient : BaseFormDev
    {
        bizCustomerClient bzCustomerClient;
        public frmCustomerClient()
        {
            InitializeComponent();
        }
        private string formSta;
        public string FormSta
        {
            set
            {
                formSta = value;
                switch (formSta)
                {
                    case "A":
                        lblBar.Caption = "新建状态";
                        break;
                    case "E":
                        lblBar.Caption = "编辑状态";
                        break;
                    default:
                        break;
                }
            }
            get
            {
                return formSta;
            }
        }
        private void frmCustomerClient_Load(object sender, EventArgs e)
        {
            bzCustomerClient = new bizCustomerClient(global.basSysPara);
            #region 下拉数据
            cboCustomer_Id.DataSource = dataProcessor.CopyIList<etyCustomer>(global.ilCustomer);
            cboClient_Id.DataSource = dataProcessor.CopyIList<etyClient>(global.ilClient); 
            cboEnable_Flag.DataSource = dataProcessor.CopyIList<TT.ORM.etyCodeRef>(global.ilCustomerEnabled);
            #endregion

            #region 表格数据
            ggcCustomerClient.GridView = this.gridView1;
            ggcCustomerClient.GridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.Click;
            string sMapingCols = "CUSTOMER_ID,CUSTOMER_NAM,CLIENT_ID,CLIENT_NAM,ENABLE_FLAG";
            string sMapingHeaderTxt = "客户代码,客户名称,办单人代码,办单人名称,生效状态";
            ggcCustomerClient.ColumnsMappingCreate(sMapingCols, sMapingHeaderTxt);
            this.winUtil.LoadGridLayout(ggcCustomerClient);

            RefreshDate();
            #endregion

            FormSta = "A";
            bar3.Text = "状态栏";
        }


        //显示行号
        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshDate();
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            NewData();
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveDate();
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteDate();
        }

        private void btnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.gridView1.OptionsFind.AlwaysVisible = !this.gridView1.OptionsFind.AlwaysVisible;
        }

        private void btnExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            F12();
        }

        public override Dev.Control.etyQuickKey RefreshDate()
        {
            srcBinding.DataSource = bzCustomerClient.Get_CustomerClient(new etyCustomerClient());
            ggcCustomerClient.DataSource = srcBinding;
            ggcCustomerClient.AutoColumnWidth();
            return base.RefreshDate();
        }

        public override etyQuickKey NewData()
        {
            FormSta = "A";
            etyCustomerClient ey = srcBinding.Current as etyCustomerClient;
            if (ey != null)
            {
                TT.Common.formProcess.SetFormEntity(ey, splitContainer.Panel1);

                ClearForm();
            }
            return base.NewData();
        }

        public override etyQuickKey DeleteDate()
        {
            etyCustomerClient ey = srcBinding.Current as etyCustomerClient;
            if (ey == null)
                return null;
            if (TT.HelpInfo.MsgBox.ShowQuestionBox("是否删除选中的记录？"))
            {
                if (bzCustomerClient.Delete_CustomerClient(ey).Exec_Success)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("删除成功");
                    lblBar.Caption = "";
                    RefreshDate();
                }
            }
            return base.DeleteDate();
        }

        public override etyQuickKey SaveDate()
        {
            etyCustomerClient ey = new etyCustomerClient();
            TT.Common.formProcess.GetFormEntity(ey, splitContainer.Panel1);
            if (check_data(ey))
            {
                daoResult rst;
                if (FormSta == "A")
                    rst = bzCustomerClient.Insert_CustomerClient(ey);
                else
                    rst = bzCustomerClient.Update_CustomerClient(ey);
                if (rst.Exec_Success)
                {
                    TT.HelpInfo.MsgBox.ShowSuccessBox("保存成功");
                    RefreshDate();
                }
                else
                {
                    TT.HelpInfo.MsgBox.ShowWarnBox(rst.Error_String);
                    formProcess.SetControlFocus(rst.Focus_Control, splitContainer.Panel1);
                }
            }
            return base.SaveDate();
        }

        #region 函数
        private void ClearForm()
        {
            cboCustomer_Id.SelectedIndex = -1;
            cboClient_Id.SelectedIndex = -1;
            cboEnable_Flag.SelectedIndex = -1;
        }
        private bool check_data(etyCustomerClient ey)
        {
            if (string.IsNullOrEmpty(ey.CUSTOMER_ID.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("客户ID号不能为空");
                cboCustomer_Id.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.CLIENT_ID.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("办单人ID号不能为空");
                cboClient_Id.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(ey.ENABLE_FLAG.Trim()))
            {
                TT.HelpInfo.MsgBox.ShowWarnBox("生效状态不能为空");
                cboEnable_Flag.Focus();
                return false;
            }
            return true;
        }
        #endregion

        private void ggcCustomerClient_Click(object sender, EventArgs e)
        {
            etyCustomerClient ey = srcBinding.Current as etyCustomerClient;
            if (ey == null)
                return;
            TT.Common.formProcess.SetFormEntity(ey, splitContainer.Panel1);
            cboCustomer_Id.GetCodeText();
            cboClient_Id.GetCodeText();
            cboEnable_Flag.GetCodeText(); 
            FormSta = "E";
        }

        private void cboCustomer_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtCustomer_Nam.Text = cboCustomer_Id.GetValue("CUSTOMER_NAM");
        }

        private void cboClient_Id_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtClient_Nam.Text = cboClient_Id.GetValue("CLIENT_NAM");

        }

    }
}
